package cn.com.dycc.sys_manage.logsysmanagement.service;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import cn.com.dycc.sys_manage.logsysmanagement.dao.OperateLogDao;
import cn.com.dycc.sys_manage.logsysmanagement.entity.LoginCount;
import cn.com.dycc.sys_manage.logsysmanagement.entity.OperateLog;
import cn.com.dycc.sys_manage.usersysmanagement.dao.UserDao;
import cn.com.dycc.sys_manage.usersysmanagement.entity.User;
import cn.com.dycc.util.PropertiesUtil;
import cn.com.dycc.util.WriterFile;
/**
 * 
 * Description: ��־service��ʵ��
 * Author: Liket
 * Version: 1.0
 * Create Date Time: 2017��8��17�� ����11:24:42.
 * Update Date Time: 
 * @see
 */
@Service("operateLogService")
public class OperateLogServiceImpl implements OperateLogService{
	@Resource
	private OperateLogDao operateLogDao;
	
	@Resource
	private UserDao userDao;
	
	public List<Map<String, Object>> findLogByPage(Integer page) throws LogNotFoundException, IOException {
		if(page==null){
			throw new LogNotFoundException("页码为空");
		}
		String str = PropertiesUtil.readProperties("size");
		int size =Integer.parseInt(str);
		int start = (page-1)*size;
		List<Map<String, Object>> list = operateLogDao.findLogByPage(start, size);
		return list;
	}

	public List<Map<String, Object>> list(HttpServletRequest req) throws LogNotFoundException {
		HttpSession session = req.getSession();
		List<Integer> role_nos = (List<Integer>) session.getAttribute("role_no");
//		System.out.println(role_nos);
		boolean ok = true;
		for(Integer role_no:role_nos){
			if(role_no==9){
				ok = false;
			}
		}
		if(ok){
			throw new LogNotFoundException("仅管理员有权限查看日志");
		}
		List<Map<String, Object>> list = operateLogDao.listLog();
		if(list.isEmpty()){
			throw new LogNotFoundException("查询失败");
		}
		return list;
	}

	public List<Map<String, Object>> listLogByParam(OperateLog log,HttpServletRequest req) throws LogNotFoundException {
		HttpSession session = req.getSession();
		List<Integer> role_nos = (List<Integer>) session.getAttribute("role_no");
	//	System.out.println(role_nos);
		boolean ok = true;
		for(Integer role_no:role_nos){
			if(role_no==9){
				ok = false;
			}
		}
		if(ok){
			throw new LogNotFoundException("仅管理员有权限查看日志");
		}
		String account = log.getAccount();
		String username = log.getUsername();
		Date login_date = log.getLogin_date();
		String ip = log.getIp();
		String operate = log.getOperate();
		if((account==null||account.trim().isEmpty())
				&&(operate==null||operate.trim().isEmpty())
				&&(username==null||username.trim().isEmpty())
				&&(ip==null||ip.trim().isEmpty())
				&&login_date==null){
			throw new LogNotFoundException("");
		}
		Map<String,Object> param = new HashMap<String,Object>();
		if(account!=null&&!account.trim().isEmpty()){
			param.put("account", account);
		}
		if(username!=null&&!username.trim().isEmpty()){
			param.put("username", username);
		}
		if(login_date!=null){
			param.put("login_date", login_date);
		}
		if(ip!=null&&!ip.trim().isEmpty()){
			param.put("ip", ip);
		}
		if(operate!=null&&!operate.trim().isEmpty()){
			param.put("operate", operate);
		}	
		
//		System.out.println(log);
		
		List<Map<String, Object>> list = operateLogDao.listLogByParam(param);
//		if(list.isEmpty()){
//			throw new LogNotFoundException("");
//		}
		return list;
	}

	public boolean addAppLog(String logs) throws LogNotFoundException {
		try {
			WriterFile.WriteStringToFile(logs);			
		} catch (Exception e) {
			throw new LogNotFoundException("添加日志失败");
		}			
		return true;
	}

	public boolean deleteLimit(String account, HttpServletRequest req) throws LogNotFoundException {
		if(account==null||account==""){
			throw  new LogNotFoundException("请输入用户账号名");
		}
		User user = userDao.findUserByAccount(account);
		if(user==null){
			throw new LogNotFoundException("该用户不存在");
		}
		HttpSession session = req.getSession();
		List<String> role_nos = (List<String>) session.getAttribute("role_no");
		//System.out.println(role_nos);
		for(String role_no:role_nos){
			if("0".equals(role_no)||"9".equals(role_no)){				
				int n = operateLogDao.deleteLimit(account);
				if(n<0){
					throw new LogNotFoundException("系统繁忙，请稍后重试");
				}
			}else{
				throw  new LogNotFoundException("您不是管理员，无法使用此功能");
			}
		}		
		return true;
	}

	public LoginCount countLogin(HttpServletRequest req) throws LogNotFoundException {
		HttpSession session = req.getSession();
		String user_id = (String)session.getAttribute("id");
		if(user_id==null){
			throw  new LogNotFoundException("超时，请重新登录");
		}
		String countWeek = operateLogDao.countWeek();
		String countAll = operateLogDao.countAll();
		LoginCount lc = new LoginCount();
		lc.setCountAll(countAll);
		lc.setCountWeek(countWeek);
		
		return lc;
	}

	

}
